from django import http
from django.core.paginator import Paginator, EmptyPage
from django.shortcuts import render
from django.views import View

from apps.boke_content.models import Blog, Category
from apps.index.models import Carousel
from libs.qiniuyun.code import storage
from utils import constants
from utils.response_code import RETCODE
from utils.views import LoginRequiredJSONMixin


class IndexView(View):

    def get(self, request, page_num):
        username = request.COOKIES.get('username')

        blog_content = Blog.objects.filter(carousel__isnull=True).order_by('-create_time')
        blogs = Blog.objects.filter(carousel__isnull=True).order_by('-read_count')[:5]
        recommends = Blog.objects.filter(carousel__isnull=True, is_recommend=True)

        paginator = Paginator(blog_content, constants.GOODS_LIST_LIMIT)

        carousel = Carousel.objects.all()

        categories = Category.objects.all()

        if not page_num:
            page_num = 1

        try:
            page_blogs = paginator.page(page_num)
        except EmptyPage:
            return http.HttpResponseNotFound('empty page')

        total_page = paginator.num_pages

        data = {
            'username': username,
            'blogs': blogs,
            'page_num': page_num,
            'page_blogs': page_blogs,
            'total_page': total_page,
            'carousels': carousel,
            'recommends': recommends,
            'categories': categories,

        }
        return render(request, 'base_index.html', context=data)


class MyIndexView(LoginRequiredJSONMixin, View):
    def get(self, request, page_num, category_id):

        username = request.COOKIES.get('username')

        blog_content = Blog.objects.filter(carousel__isnull=True, category_id=category_id).order_by('-create_time')
        blogs = Blog.objects.filter(carousel__isnull=True).order_by('-read_count')[:5]
        recommends = Blog.objects.filter(carousel__isnull=True, is_recommend=True)

        paginator = Paginator(blog_content, constants.GOODS_LIST_LIMIT)

        carousel = Carousel.objects.all()

        categories = Category.objects.all()

        if not page_num:
            page_num = 1

        try:
            page_blogs = paginator.page(page_num)
        except EmptyPage:
            return http.HttpResponseNotFound('empty page')

        total_page = paginator.num_pages

        try:
            category = Category.objects.get(id=category_id)
        except Category.DoesNotExist:
            return http.JsonResponse({'code_s': RETCODE.DBERR, 'errmsg': '错误'})

        context = {
            'username': username,
            'blogs': blogs,
            'page_num': page_num,
            'page_blogs': page_blogs,
            'total_page': total_page,
            'carousels': carousel,
            'recommends': recommends,
            'categories': categories,
            'image_url': request.user.avatar_url,
            'category_id': category_id,
            'category': category
        }

        return render(request, 'base_index3.html', context=context)


class HeadImage(View):
    def post(self, request):
        try:
            # 读取上传的文件
            file = request.FILES.get('file')
            avatar_file = file.read()  # 读取数据
        except Exception as e:
            return http.JsonResponse({'code_s': RETCODE.IMAGEERROR, 'errmsg': '获取图片失败'})

        try:
            url = storage(avatar_file)
            image = "http://q2e9jznlm.bkt.clouddn.com/" + url
            request.user.avatar_url = image
            request.user.save()
        except Exception as e:
            return http.JsonResponse({'code_s': RETCODE.IMAGEERROR, 'errmsg': '上传七牛云失败'})

        return http.JsonResponse({'code_s': RETCODE.OK, 'errmsg': '上传成功'})
